package com.firesrc.web.dwr;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import com.firesrc.bean.MailHost;
import com.firesrc.mail.send.MailSendManagerFactory;
import com.firesrc.mail.send.MailSender;
import com.firesrc.service.MailHostService;
import com.firesrc.service.impl.MailHostServiceImpl;

public class MailHostAction {
	public MailHost getMailHostByMail(String mail) {
		String hostName = getHostNameByMail(mail);
		Map<String, MailHost> map = getInitHostConfigMap();
		MailHost mailHost = map.get(hostName);
		if (mailHost == null) {
			MailHost mailHost2 = new MailHost();
			mailHost2.setMail(mail);
			return mailHost2;
		}
		int indexOf = mail.indexOf("@");
		if (indexOf > 0) {
			mailHost.setUserName(mail.substring(0, indexOf));
		}
		mailHost.setMail(mail);
		return mailHost;
	}

	private Map<String, MailHost> getInitHostConfigMap() {
		Map<String, MailHost> map = new HashMap<String, MailHost>();

		MailHost mailHost = new MailHost();
		mailHost.setHostName("smtp.qq.com");
		mailHost.setKind("qq");
		mailHost.setSsl(false);
		mailHost.setSendOnceMaxCount(10);
		mailHost.setSendDayMaxCount(100);
		map.put("qq.com", mailHost);

		mailHost = new MailHost();
		mailHost.setHostName("smtp.gmail.com");
		mailHost.setKind("gmail");
		mailHost.setSendOnceMaxCount(10);
		mailHost.setSendDayMaxCount(100);
		mailHost.setSsl(true);
		map.put("gmail.com", mailHost);

		mailHost = new MailHost();
		mailHost.setHostName("smtp.21cn.com");
		mailHost.setKind("21cn");
		mailHost.setSsl(false);
		mailHost.setSendOnceMaxCount(10);
		mailHost.setSendDayMaxCount(100);
		map.put("21cn.com", mailHost);

		mailHost = new MailHost();
		mailHost.setHostName("mail.juran.com.cn");
		mailHost.setKind("juran");
		mailHost.setSsl(false);
		mailHost.setSendOnceMaxCount(10);
		mailHost.setSendDayMaxCount(100);
		map.put("juran.com.cn", mailHost);

		mailHost = new MailHost();
		mailHost.setHostName("smtp.sohu.com");
		mailHost.setKind("sohu");
		mailHost.setSsl(false);
		mailHost.setSendOnceMaxCount(10);
		mailHost.setSendDayMaxCount(100);
		map.put("sohu.com", mailHost);

		mailHost = new MailHost();
		mailHost.setHostName("smtp.sina.com");
		mailHost.setKind("sina");
		mailHost.setSsl(false);
		mailHost.setSendOnceMaxCount(5);
		mailHost.setSendDayMaxCount(100);
		map.put("sina.com", mailHost);

		mailHost = new MailHost();
		mailHost.setHostName("smtp.sina.com");
		mailHost.setKind("sina");
		mailHost.setSsl(false);
		mailHost.setSendOnceMaxCount(5);
		mailHost.setSendDayMaxCount(100);
		map.put("sina.cn", mailHost);

		mailHost = new MailHost();
		mailHost.setHostName("smtp.163.com");
		mailHost.setKind("163");
		mailHost.setSsl(true);
		mailHost.setSendOnceMaxCount(5);
		mailHost.setSendDayMaxCount(100);
		map.put("163.com", mailHost);

		mailHost = new MailHost();
		mailHost.setHostName("smtp.mail.yahoo.com");
		mailHost.setKind("yahoo");
		mailHost.setSsl(false);
		mailHost.setSendOnceMaxCount(5);
		mailHost.setSendDayMaxCount(100);
		map.put("yahoo.com", mailHost);

		return map;
	}

	public String getHostNameByMail(String mail) {
		return mail.substring(mail.indexOf("@") + 1, mail.length());

	}

	public MailHost getMailHostById(String id) {
		MailHostService service = new MailHostServiceImpl();
		return service.getMailHostById(id);
	}

	public MailHost save(MailHost mailHost) {
		MailHostService service = new MailHostServiceImpl();
		return service.save(mailHost);
	}

	public Map checkMail(MailHost mailHost, String toMail) {
		MailSender mailSender = MailSendManagerFactory.getMailSenderFromMailHost(mailHost);
		String singleSendMail = mailSender.SingleSendMail(toMail);
		Map map = new HashMap();
		map.put("result", singleSendMail);
		map.put("id", mailHost.getId());
		map.put("mail", mailHost.getMail());
		return map;
	}

	public Map checkMailByThread(final MailHost mailHost, final String toMail) {
		Callable<String> callable = new Callable<String>() {
			@Override
			public String call() throws Exception {
				MailSender mailSender = MailSendManagerFactory.getMailSenderFromMailHost(mailHost);
				return mailSender.SingleSendMail(toMail);
			}
		};

		FutureTask<String> futureTask = new FutureTask<String>(callable);
		futureTask.run();
		Map map = new HashMap();
		String result = "发生未知错误";
		try {
			result = futureTask.get();
		} catch (Exception e) {
		}
		map.put("result", result);
		map.put("id", mailHost.getId());
		map.put("mail", mailHost.getMail());
		return map;
	}
}
